﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace TicTacToeX {
	public static class Table {

		public enum Player {
			One, Two
		}

		public enum GameState {
			IsDraw, IsPlaying, PlayerXWins, PlayerOWins
		}

		public static Int16 Rows;
		public static Int16 Collumns;
		public static Char[,] AnchorCell;
		public static Vector2 Centralize;
		public static Vector2 Corner;
		public static Player CurrentPlayer;
		public static GameState CurrentGameState;

		/// <summary>
		/// Use this method to create a table
		/// </summary>
		/// <param name="rows">number of rows</param>
		/// <param name="collumns">number of collumns</param>
		public static void Initialize(Int16 rows, Int16 collumns) {
			Rows          = rows;
			Collumns      = collumns;
			Centralize    = new Vector2(GraphicsSettings.Width * 0.5f,GraphicsSettings.Height * 0.5f);
			CurrentPlayer = Player.One; //! Change to a method
			AnchorCell    = new Char[Rows, Collumns];

			for(int nY = 0;nY < Collumns;nY++) {
				for(int nX = 0;nX < Rows;nX++) {
					AnchorCell[nX,nY] = ' ';
				}
			}
		}

		public static void ApplyMove(Vector2 finalPos, Vector2 gridPos, Player currentPlayer, Player nextPlayer, Char piece){
			if(Input.VerifyMouseClick(finalPos)) {
				if(Table.GetAvaibleMove(gridPos)) {
					Table.AnchorCell[(int)gridPos.X,(int)gridPos.Y] = piece;
					Table.CurrentPlayer = nextPlayer;
				} else {
					Table.CurrentPlayer = currentPlayer;
				}
				UpdateMove(gridPos,piece);
			}
		}

		public static bool GetAvaibleMove(Vector2 gridPos) {
			return AnchorCell[(int)gridPos.X,(int)gridPos.Y] == ' ' ? true : false;			
		}

		private static void UpdateMove(Vector2 gridPos,Char piece) {
			if(CheckWinsLine(piece,(int)gridPos.Y) || CheckWinsCollumn(piece,(int)gridPos.X) || CheckDiagonal(piece) || CheckInvertDiagonal(piece)) {
				switch(piece) {
					case 'X':
						CurrentGameState = GameState.PlayerXWins;
						break;
					case 'O':
						CurrentGameState = GameState.PlayerOWins;
						break;
				}
			} else if(TestForEmptyCells()) {
				switch(CurrentGameState) {
					case GameState.PlayerXWins:
						CurrentGameState = GameState.PlayerXWins;
						break;

					case GameState.PlayerOWins:
						CurrentGameState = GameState.PlayerOWins;
						break;

					default:
						CurrentGameState = GameState.IsPlaying;
						//GetAvaibleMove();
						break;
				}
			} else {
				CurrentGameState = GameState.IsDraw;
			}

		}

		private static bool TestForEmptyCells()
		  {            
				for (int nY = 0; nY < Collumns; nY++)
				{
					 for (int nX = 0; nX < Rows; nX++)
					 {
						  if (AnchorCell[nX, nY] == ' ')
								return true;
					 }
				}
				return false;
		  }

		private static bool CheckWinsLine(char piece, int posY)
		  {
				bool bnComplete = false;
				for (int nX = 0; nX < Collumns; nX++)
				{
					 bnComplete = true;
					 if (AnchorCell[nX, posY] != piece)
					 {
						  bnComplete = false;
						  break;
					 }
				}
				if (bnComplete)
					 return true;

				return false;
		  }

		private static bool CheckWinsCollumn(char piece, int posX)
		  {
				bool bnComplete = false;
				for (int nY = 0; nY < Collumns; nY++)
				{
					 bnComplete = true;
					 if (AnchorCell[posX, nY] != piece)
					 {
						  bnComplete = false;
						  break;
					 }
				}
				if (bnComplete)
					 return true;

				return false;
		  }

		private static bool CheckDiagonal(char piece)
		  {
				bool bnComplete = false;
				for (int nC = 0; nC < Collumns; nC++)
				{
					 bnComplete = true;
					 if (AnchorCell[nC, nC] != piece)
					 {
						  bnComplete = false;
						  break;
					 }
				}
				if (bnComplete)
					 return true;

				return false;
		  }

		private static bool CheckInvertDiagonal(char piece)
		  {
				bool bnComplete = false;
				for (int nC = 0; nC < Collumns; nC++)
				{
					 bnComplete = true;
					 if (AnchorCell[Collumns - nC - 1, nC] != piece)
					 {
						  bnComplete = false;
						  break;
					 }
				}
				if (bnComplete)
					 return true;

				return false;
		  }

		/*
		  public int Minimax(int deph, int bestScore,Player player)
		  {
				int score;            

				if (!TestForEmptyCells() || deph == 0)
				{
					 return 0;
				}
				if (m_gameState == GameState.PlayerOWins)
				{
					 return -999;
				}
				if (m_gameState == GameState.PlayerXWins)
				{
					 return 999;
				}


				if (m_player == Player.Human)
				{
					 for (int nY = 0; nY < m_nSide; nY++)
					 {
						  for (int nX = 0; nX < m_nSide; nX++)
						  {
								score = Minimax(deph - 1,bestScore, Player.Computer);
								if (score > bestScore)
								{
									 bestScore = score;
									 //m_achCells[posX, posY] = 'X';
								}
						  }
					 }

				}
				else
				{
					 for (int nY = 0; nY < m_nSide; nY++)
					 {
						  for (int nX = 0; nX < m_nSide; nX++)
						  {
								score = Minimax(deph - 1,bestScore,Player.Human);
								if (score > bestScore)
								{
									 bestScore = score;
									 m_achCells[nX, nY] = 'O';
								}  
						  }
					 }
 
				}
				return bestScore;
		  }
*/

		 /* public GameState WhoWins ()
		  {
				return m_gameState;
		  }
		 */
	 }
}
